package com.zd.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zd.dto.Exposer;
import com.zd.dto.SeckillExecution;
import com.zd.dto.SeckillResult;
import com.zd.entity.Seckill;
import com.zd.enums.SeckillStateEnum;
import com.zd.exception.RepeatKillException;
import com.zd.exception.SeckillCloseExecption;
import com.zd.service.SeckillService;

@Controller
@RequestMapping("/seckill")		//url:/模块/资源/{id}/细分
public class SeckillController extends BaseController {
	@Autowired
	private SeckillService seckillService;
	
	@RequestMapping(value="/list",method={RequestMethod.GET})
	public String list(Model model){
		List<Seckill> list = seckillService.getSeckillList();
		model.addAttribute("list",list);
		return "list";
	}
	@RequestMapping(value="/{seckillId}/detail",method=RequestMethod.GET)
	public String detail(@PathVariable("seckillId") Long seckillId,Model model ){
		if(seckillId == null){
			return "redirect:/seckill/list";
		}
		Seckill seckill = seckillService.getById(seckillId);
		if(seckill == null){
			return "forward:/seckill/list";
		}
		model.addAttribute("seckill",seckill);
		return "detail";
	}
//	ajax json
	@ResponseBody
	@RequestMapping(value="/{seckillId}/exposer",method = RequestMethod.POST,
			produces = "application/json; charset=UTF-8")
	public SeckillResult<Exposer> exposer(@PathVariable("seckillId") Long seckillId){
		SeckillResult<Exposer> result;
		try {
			Exposer exposer = seckillService.exportSeckillUrl(seckillId);
			result = new SeckillResult<Exposer>(true, exposer);
		} catch (Exception e) {
			e.printStackTrace();
			result = new SeckillResult<Exposer>(false, e.getMessage());
			// TODO: handle exception
		}
		return result;
		
	}
	
	@RequestMapping(value="/{seckillId}/{md5}/execution",
			produces = "application/json; charset=UTF-8")
	@ResponseBody
	public SeckillResult<SeckillExecution> excute(@PathVariable("seckillId") Long seckillId,
			@PathVariable(value="md5") String md5,@CookieValue(value="killPhone",required=false) Long phone){
		if(phone == null){
			return new SeckillResult<SeckillExecution>(false, "未注册");
		}
		
		SeckillResult<SeckillExecution> result;
		try {
			SeckillExecution execution = seckillService.executeSeckill(seckillId, phone, md5);
			return new SeckillResult<SeckillExecution>(true, execution);
		} catch (RepeatKillException e) {
			// TODO: handle exception
			SeckillExecution execution = new SeckillExecution(seckillId, SeckillStateEnum.REPEAT_KILL);
			return new SeckillResult<SeckillExecution>(true, execution);
		} catch (SeckillCloseExecption e) {
			// TODO: handle exception
			SeckillExecution execution = new SeckillExecution(seckillId,SeckillStateEnum.END);
			return new SeckillResult<SeckillExecution>(true, execution);
		} catch (Exception e) {
			e.printStackTrace();
			SeckillExecution execution = new SeckillExecution(seckillId, SeckillStateEnum.INNER_ERROR);
			return new SeckillResult<SeckillExecution>(true, execution);
			// TODO: handle exception
		}
		
	}
	@ResponseBody
	@RequestMapping(value="/time/now",method=RequestMethod.GET)
	public SeckillResult<Long> time(){
		Date now = new Date();
		return new SeckillResult<Long>(true, now.getTime());
	}
}
